package com.zerdoor.service.impl;

import com.zerdoor.dao.RaceDetailDao;
import com.zerdoor.service.ICommonPayService;
import com.zerdoor.service.RaceDetailService;
import com.zerdoor.service.RaceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/3.
 */
@Service
public class RaceDetailServiceImpl implements RaceDetailService {

    @Autowired
    public RaceDetailDao raceDetailDao;
    @Autowired
    private ICommonPayService iCommonPayService;
    @Autowired
    private RaceOrderService raceOrderService;
    @Override
    public Map<String, Object> getRaceDetailByRaceId(Integer raceId) {
        return raceDetailDao.getRaceDetailByRaceId(raceId);
    }

    @Override
    public List<Map<String, Object>> getRaceManuscript(Integer raceId) {
        return raceDetailDao.getRaceManuscript(raceId);
    }

    @Override
    public List<Map<String, Object>> getProcesses() {
        return raceDetailDao.getProcesses();
    }

    @Override
    public Integer getUserRaceManuscriptByRaceId(Integer raceId, Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("raceId", raceId);
        map.put("userId", userId);
        return raceDetailDao.getUserRaceManuscriptByRaceId(map);
    }

    @Override
    public List<Map<String, Object>> getRaceManuscriptPrize(Integer raceId) {
        return raceDetailDao.getRaceManuscriptPrize(raceId);
    }

    @Override
    public void updateWinRace(int raceId, String ids, int userId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("raceId", raceId);
        map.put("userId", userId);

        if("".equals(ids)){
            throw new Exception("未选中中奖者");
        }
        //验证订单装态
        //验证是否为该用户订单
        Map<String, Object> order = raceOrderService.getRaceOrderById(raceId,userId);
        if(order == null){
            throw new Exception("此竞赛活动不存在");
        }
        //是否扣除手续费判断
        boolean isFee = true;
        BigDecimal fee = new BigDecimal(order.get("fee").toString());
        if(fee.compareTo(new BigDecimal(0)) == 0){
            isFee = false;
        }
        //是否扣除推广费判断
        boolean isPopur =true;
        BigDecimal popur = new BigDecimal(order.get("popur").toString());
        if(popur.compareTo(new BigDecimal(0)) == 0){
            isPopur = false;
        }

        List<Map<String,Object>> list = getAritistByPrize(ids);
        for(Map<String,Object> temp : list){
            //验证中奖艺术家是否和该这等级设置中奖数相等
            int artistIds = temp.get("artistIds").toString().split(",").length;
            int num = raceDetailDao.getPrizeById(Integer.parseInt(temp.get("prizeId").toString()));
            if(artistIds != num){
                throw new Exception("奖项完全未设置!");
            }
            temp.put("raceId",raceId);
            raceDetailDao.updateWinRace(temp);
            raceDetailDao.updatePrizeWinNum(temp);
        }
        //调用钱包接口扣除
        BigDecimal totalPrice = new BigDecimal(order.get("totalPrice").toString());
        iCommonPayService.updateRaceMoney(list,isFee,isPopur,raceId,totalPrice,userId,Integer.parseInt(order.get("orderRaceId").toString()));
        //验收付款
        raceDetailDao.updateRaceProgress(map);
        //改变订单状态
        raceOrderService.updateOrderRaceStatus(raceId);
    }

    @Override
    public List<Map<String, Object>> getPrizeByRaceId(int raceId) {
        return raceDetailDao.getPrizeByRaceId(raceId);
    }


    /**
     * 对获奖数据进行处理
     * @param ids
     * @return
     */
    private List<Map<String, Object>> getAritistByPrize(String ids){
        //获奖等级
        List<Integer> prizeLevel = new ArrayList<Integer>();

        //数据处理-处理有多少等级中奖
        String[] arr = ids.split(",");
        for(int i=0;i<arr.length;i++){
            String[] arr2 = arr[i].split(":");
            if(!prizeLevel.contains(Integer.parseInt(arr2[1].toString()))){
                prizeLevel.add(Integer.parseInt(arr2[1].toString()));
            }
        }

        //整理数据-每个等级有哪些艺术家
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for(Integer prizeId:prizeLevel){
            Map<String, Object> map2 = new HashMap<String, Object>();
            String artistIds = "";
            //该等级获奖者数量
            int winNum=0;
            for(int i=0;i<arr.length;i++){
                String[] arr2 = arr[i].split(":");
                if(prizeId.intValue() == Integer.parseInt(arr2[1].toString())){
                    if("".equals(artistIds)){
                        artistIds += arr2[0].toString();
                    }else{
                        artistIds += ","+arr2[0].toString();
                    }
                    winNum++;
                }
            }

            map2.put("prizeId", prizeId);
            map2.put("artistIds",artistIds);
            map2.put("winNum",winNum);
            list.add(map2);
        }
        return list;
    }
}
